#!/bin/bash

# Simple script to make sure the developer console is open and
# the qrc's for the html/js are generated.

###########################
# Environment
###########################

PWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export QTWEBENGINE_REMOTE_DEBUGGING=127.0.0.1:12345
DIR=/tmp/chrome-devel-configuration

###########################
# Generate Resources
###########################

cd ${PWD}/../py_trees_js && ./gen.bash
cd ${PWD}/../py_trees_js/viewer && ./gen.bash

###########################
# Setup
###########################

# Clear out the old user data
rm -rf ${DIR}

# Sync'ing to a user directory doesn't seem to be necessary if I use $OPTIONS1

#if [ ! -d "${DIR}" ]; then
#  mkdir -p ${DIR}
#  rsync -av --delete --exclude=/Singleton* --exclude=/Session* ~/.config/google-chrome/ ${DIR}
#fi

###########################
# Launch
###########################
OPTIONS1="--no-first-run --activate-on-launch --no-default-browser-check --allow-file-access-from-files"
# These are to work around chrome 80+ incompatibility: 
#  https://stackoverflow.com/questions/60182668/chrome-devtools-inspector-showing-blank-white-screen-while-debugging-with-samsun
OPTIONS2="--enable-blink-features=ShadowDOMV0 --enable-blink-features=CustomElementsV0"
google-chrome ${OPTIONS1} ${OPTIONS2} --user-data-dir=${DIR} --app=http://${QTWEBENGINE_REMOTE_DEBUGGING} > /dev/null 2>&1 &
pid=$!

py-trees-demo-viewer

kill -s 9 $pid > /dev/null 2>&1 || exit 0
